﻿using System;
using System.Windows.Forms;
using DbManager;
using DbManager.DataManager;
using DbManager.QueryBuilder.DbEntity.Komnata;

namespace TAProject.UI.Service.Room
{
    public partial class FormKomnataEdit : FormTemplate
    {
        private readonly Form _parentForm;
        private readonly IDataManager _dataManager;
        private readonly IConnectionManager _connetctionManager;
        private readonly AEntityKomnata _aEntityKomnata;
        private readonly bool _isUpdate;

        public FormKomnataEdit(Form parentForm, IDataManager dataManager, IConnectionManager connetctionManager,
                               AEntityKomnata aEntityKomnata, bool isUpdate)
            : base(parentForm)
        {
            InitializeComponent();

            _parentForm = parentForm;
            _dataManager = dataManager;
            _connetctionManager = connetctionManager;
            _aEntityKomnata = aEntityKomnata;
            _isUpdate = isUpdate;

            FillComboBox();

            if (isUpdate)
            {
                if (string.IsNullOrEmpty(_aEntityKomnata.Klass))
                {
                    MessageBox.Show("Ошибка получения даных об изменяемом объекте.");
                    FormClose();
                }
                else
                {
                    textBoxKlass.Text = _aEntityKomnata.Klass;
                    comboBoxMesta.SelectedIndex = _aEntityKomnata.Mesta == 0 ? 0 : _aEntityKomnata.Mesta - 1;
                }
            }

            _parentForm.Enabled = false;
        }

        private void FormClose()
        {
            _parentForm.Enabled = true;
            Close();
        }

        private void ButtonCancelClick(object sender, EventArgs e)
        {
            FormClose();
        }

        private void ButtonSubmitClick(object sender, EventArgs e)
        {
            var mesta = ((ComboBoxFiller<int>) comboBoxMesta.SelectedItem).Value;
            var klass = textBoxKlass.Text;

            if (string.IsNullOrEmpty(klass))
            {
                MessageBox.Show("проверьте правильность заполнения полей.");
            }
            else
            {
                _aEntityKomnata.Klass = klass;
                _aEntityKomnata.Mesta = mesta;

                var rowAffected = _dataManager.ManageDataInDb(
                    _isUpdate ? _aEntityKomnata.GetUpdateQuery() : _aEntityKomnata.GetInsertQuery(),
                    _connetctionManager.GetConnection());

                if (rowAffected == 0)
                    MessageBox.Show("Ошибка сохранения данных.");
                else
                    FormClose(); 
            }
        }

        private void FillComboBox()
        {
            comboBoxMesta.Items.AddRange(new object[]
                                             {
                                                 new ComboBoxFiller<int>("0", 0),
                                                 new ComboBoxFiller<int>("1", 1),
                                                 new ComboBoxFiller<int>("2", 2),
                                                 new ComboBoxFiller<int>("3", 3),
                                                 new ComboBoxFiller<int>("4", 4),
                                                 new ComboBoxFiller<int>("5", 5)
                                             });
            comboBoxMesta.SelectedIndex = 0;
        }
    }
}